cc.Class({
    extends: cc.Component,

    properties: {
        scoreText: cc.Label,
    },

    init: function (game) {
        this.game = game;
        this._score = 0;
    },
    
    // 获取分数
    getScore: function(){
        return this._score;
    },
   
    // 设置分数
    setScore: function(score){
        this._score = score;
        this._updateScore();
    },
    
    // 增加分数
    addScore: function(){
        this._score += 1;
        this._updateScore();
        
        //this.game.soundMng.playScoreSound();
    },
    
    // 更新分数
    _updateScore: function(){
        this.node.getComponent(cc.Label).string = "分数:" + this._score;
    },

    GetTableVideo() {
        // 生成一个随机整数，范围在1到6之间
        let randomInt = Math.floor(Math.random() * 6) + 1;
        let TableScreenADStr = "k8b67ifdfg3k578j8a";

        // 根据随机数选择对应的表格屏幕广告字符串
        switch (randomInt) {
            case 1:
                TableScreenADStr = "k8b67ifdfg3k578j8a";
                break;
            case 2:
                TableScreenADStr = "1lcnff089p9n5di8ha";
                break;
            case 3:
                TableScreenADStr = "dbqo9n2wle265t75sa";
                break;
            case 4:
                TableScreenADStr = "65e4t19tson1p894ak";
                break;
            case 5:
                TableScreenADStr = "22king8d6cd424hed0";
                break;
            case 6:
                TableScreenADStr = "kjgsiski0mcahfb3an";
                break;
            default:
                TableScreenADStr = "kjgsiski0mcahfb3an";
                break;
        }

        // 返回选中的表格屏幕广告字符串
        return TableScreenADStr;
    },

    showRewardedAd() {
            //addScore();
            cc.director.pause();
            // 请求激励广告
            let videoAd = tt.createRewardedVideoAd({
                //adUnitId: "k8b67ifdfg3k578j8a",
                adUnitId: this.GetTableVideo(),
                multiton: true,
                multitonRewardMsg: ['更多奖励1', '更多奖励2', '更多奖励3'],
                multitonRewardTimes: 3,
                progressTip: false,
            });
            videoAd.show().then(() => {
                console.log("视频广告展示");
            });
            videoAd.onClose(res => {
                // 用户点击了【关闭广告】按钮
                // 小于 2.1.0 的基础库版本，res 是一个 undefined
                if (res && res.isEnded || res === undefined) {
                    // 正常播放结束，可以下发游戏奖励的逻辑
                    this.addScore();
                    window.maidianback(function(res) {
                        console.log("回调结果: ", res);
                    });
                    console.log(`观看成功`);
                    console.log(`watch ${res.count} video`);
                    cc.director.resume();
                    //todo：插屏广告等待播放的计时恢复...             
                }
                else {
                    // 播放中途退出，不下发游戏奖励
                    cc.director.resume();
                    //todo：插屏广告等待播放的计时恢复...    
                }
            });
        }
});
